package com.book.manager.service;

import com.book.manager.dao.CustomerDAO;
import com.book.manager.pojo.Customer;
import com.book.manager.pojo.OrderBook;
import com.book.manager.dao.OrdersDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService {
    @Autowired
    private CustomerDAO customerDAO;
    @Autowired
    private OrdersDAO ordersDAO;
    @Autowired
    private OrderBookService orderBookService;
    public List<Customer> list() {
        List<Customer> customerList = customerDAO.findAll();
        // 查询订单对应的订单簿列表
        List<OrderBook> orderBooks;

        // 遍历订单列表
        for(Customer customer:customerList){
            // 根据订单id查询订单簿列表
           int  orderId = ordersDAO.findOrderIdBySerialid(customer.getSerialid());
            // 根据订单id查询订单簿列表
            orderBooks=orderBookService.listAllByOrderId(orderId);

            // 将订单簿列表设置到订单对象中
            customer.setOrderBooks(orderBooks);
        }
        // 返回订单列表
        return customerList;
    }

    public boolean addOrUpdate(Customer customer) {
        customerDAO.save(customer);
        return true;
    }

    public void deleteById(int id) {
        customerDAO.deleteById(id);
    }
}
